package com.meixiang.adapter.shopping;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.GoodsParameterEntity;
import com.meixiang.util.TextViewUtil;
import com.meixiang.view.DashedLine;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品参数列表适配器
 *
 * @author LZRUI
 */
public class GoodsParamsAdapter extends RecyclerView.Adapter<GoodsParamsAdapter.ParamsViewHolder> {

    private List<GoodsParameterEntity> mDatas;
    private Context mContext;

    public GoodsParamsAdapter(Context context){
        this.mContext = context;
    }

    public void setDatas(List<GoodsParameterEntity> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public ParamsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_goods_params, parent, false);
        return new ParamsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ParamsViewHolder holder, int position) {
        GoodsParameterEntity bean = mDatas.get(position);
        if (bean != null) {
            holder.tvParamsName.setText(String.format(mContext.getString(R.string.parameter_str),TextViewUtil.setAlignBothSide(mContext,  bean.getParameterName(), 4)));
            holder.tvParamsInfo.setText(bean.getParameterValue());
        }
        if (position == mDatas.size() - 1) {
            holder.line.setVisibility(View.GONE);
        } else {
            holder.line.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class ParamsViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_params_name)
        TextView tvParamsName;
        @Bind(R.id.tv_params_info)
        TextView tvParamsInfo;
        @Bind(R.id.view_line)
        DashedLine line;

        ParamsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
